package com.applet.applet_shangcheng.service;

import com.applet.applet_shangcheng.dao.AddressMapper;
import com.applet.applet_shangcheng.dao.AdminMapper;
import com.applet.applet_shangcheng.dao.UserMapper;
import com.applet.applet_shangcheng.entity.Address;
import com.applet.applet_shangcheng.entity.Admin;
import com.applet.applet_shangcheng.entity.User;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AddressMapper addressMapper;

    public String selectCode(String code) {
        String userId = userMapper.selectCode(code);
        return userId;
    }

    public void createCode(String userId, String code) {
        userMapper.createCode(userId,code);
    }

    public User getUser(String userId) {
        User user =userMapper.getUser(userId);
        return user;
    }
    public List<User> getUserList() {
        List<User> userList =userMapper.getUserList();
        return userList;
    }
    public  List<Address> findByAddress(String username){
        List<Address> addressList = addressMapper.findByAddress(username);
        return addressList;
    };
    public  int deleteAddress(int id){
        int result = addressMapper.deleteAddress(id);
        return result;
    };

    @Transactional(rollbackFor = Exception.class)
    public  int updateAddress(Address address){
        String isDefault = address.getIsDefault();
        if (isDefault.equals(1)){
            String username = address.getUsername();
            String provinceid = address.getProvinceid();
            String cityid = address.getCityid();
            String address1 = address.getAddress();
            String date = provinceid+cityid+address1;
            String phone = address.getPhone();
            userMapper.updateDefaultValue(username,date,phone);
        }
        int result = addressMapper.updateAddress(address);
        return result;
    };

    @Transactional(rollbackFor = Exception.class)
    public  int insertAddress(Address address){
        String isDefault = address.getIsDefault();
        if (isDefault.equals(1)){
            String username = address.getUsername();
            String provinceid = address.getProvinceid();
            String cityid = address.getCityid();
            String address1 = address.getAddress();
            String date = provinceid+cityid+address1;
            String phone = address.getPhone();
            userMapper.updateDefaultValue(username,date,phone);
        }
        int result = addressMapper.insertAddress(address);
        return result;
    };

/*    public User findUserByName(String username) {
        return userMapper.selectByName(username);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return this.findUserByName(username);
    }*/

    public User findUserByName(String username) {
        return userMapper.selectByName(username);
    }

    public Admin findUserByAdmin(String username) {
        return userMapper.findUserByAdmin(username);
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return this.findUserByAdmin(username);
    }

    public PageResult findAll(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize );
        Page<User> page = userMapper.findAll1(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(),page.getResult() );

    }

    public PageResult findAllAdmin(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize );
        Page<User> page = userMapper.findAllAdmin(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(),page.getResult() );
    }

    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    public User selectUserId(String userId) {
        return userMapper.selectUserId(userId);
    }

    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    public int updateAdmin(Admin admin) {
       return userMapper.updateAdmin(admin);
    }

    public int insertAdmin(Admin admin) {
        return userMapper.insertAdmin(admin);
    }

    public Admin getAdmin(String username) {
        return userMapper.getAdmin(username);
    }

    public int deleteAdmin(String username) {
        return userMapper.deleteAdmin(username);
    }

    public List<Integer> findMemberCountByRegTime(List<String> months) {
        List<Integer> MemberCount = new ArrayList<>();
        for (String month : months) {
            int count =userMapper.getTime(month);
            MemberCount.add(count);
        }
        return MemberCount;
    }
}
